package edu.gdin.ecommerce.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import edu.gdin.ecommerce.model.Inventory;
import edu.gdin.ecommerce.util.Pager;

@Repository("inventoryDAO")
public class InventoryDAO extends BaseDAO {

	public Pager queryInventory(Inventory inventory, String operTime1, String operTime2, int currentPage) {
		StringBuffer hql = new StringBuffer();
		hql.append("from Inventory i where 1=1 ");
		
		if(inventory != null) {
			if(inventory.getCardIssuer() != null && inventory.getCardIssuer().getCardIssuerId() != null 
					&& inventory.getCardIssuer().getCardIssuerId() != -1 ) {
				hql.append(" and i.cardIssuer.cardIssuerId = " + inventory.getCardIssuer().getCardIssuerId());
			}
			if(inventory.getMaterialType() != null && inventory.getMaterialType() != -1) {
				hql.append(" and i.materialType = " + inventory.getMaterialType());
			}
			if(inventory.getStatus() != null && inventory.getStatus() != -1) {
				hql.append(" and i.status = " + inventory.getStatus());
			}
			if(inventory.getBatchNo() != null && !inventory.getBatchNo().equals("")) {
				hql.append(" and i.batchNo = " + inventory.getBatchNo());
			}
			if(inventory.getInventoryNo() != null && !inventory.getInventoryNo().equals("")) {
				hql.append(" and i.inventoryNo = " + inventory.getInventoryNo());
			}
			if (inventory.getFaceValue() != null) {
				hql.append(" and i.faceValue = " + inventory.getFaceValue());
			}
			
		}
		
		if(operTime1 != null && !operTime1.equals("")) {
			hql.append(" and i.createTime >= '" + operTime1 + " 00:00:00'");
		}
		if(operTime2 != null && !operTime2.equals("")) {
			hql.append(" and i.createTime <= '" + operTime2 + " 23:59:59'");
		}
		hql.append(" order by i.createTime desc");
		
		return this.getPage(hql.toString(), currentPage);
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Inventory> queryInventory(Inventory inventory, String operTime1, String operTime2) {
		StringBuffer hql = new StringBuffer();
		hql.append("from Inventory i where 1=1 ");
		
		if(inventory != null) {
			if(inventory.getCardIssuer() != null && inventory.getCardIssuer().getCardIssuerId() != null 
					&& inventory.getCardIssuer().getCardIssuerId() != -1 ) {
				hql.append(" and i.cardIssuer.cardIssuerId = " + inventory.getCardIssuer().getCardIssuerId());
			}
			if(inventory.getMaterialType() != null && inventory.getMaterialType() != -1) {
				hql.append(" and i.materialType = " + inventory.getMaterialType());
			}
			if(inventory.getStatus() != null && inventory.getStatus() != -1) {
				hql.append(" and i.status = " + inventory.getStatus());
			}
			if(inventory.getBatchNo() != null && !inventory.getBatchNo().equals("")) {
				hql.append(" and i.batchNo = " + inventory.getBatchNo());
			}
			if(inventory.getInventoryNo() != null && !inventory.getInventoryNo().equals("")) {
				hql.append(" and i.inventoryNo = " + inventory.getInventoryNo());
			}
			if (inventory.getFaceValue() != null) {
				hql.append(" and i.faceValue = " + inventory.getFaceValue());
			}
			
		}
		
		if(operTime1 != null && !operTime1.equals("")) {
			hql.append(" and i.createTime >= '" + operTime1 + " 00:00:00'");
		}
		if(operTime2 != null && !operTime2.equals("")) {
			hql.append(" and i.createTime <= '" + operTime2 + " 23:59:59'");
		}
		hql.append(" order by i.createTime desc");
		
		return (List<Inventory>) query(hql.toString());
	}
	
	
	
	public void changeStatus(String batchNo, Integer status) {
		String[] str = {status.toString(), batchNo};
		this.hibernateTemplate.bulkUpdate("update Inventory i set i.status=" + status + " where i.batchNo=" + batchNo);
//		this.hibernateTemplate.bulkUpdate("update Inventory i set i.status=? where i.batchNo=?", str);
	}
	
	
}
